package com.aimee.servlet;

import com.aimee.dao.ExamPaperDAO;
import com.aimee.entity.ClassMajorDeptShow;
import com.aimee.entity.ExamPaper;
import com.aimee.entity.ExamPaperShow;
import com.aimee.util.PageBean;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/admin/exampaper")
public class ExamPaperServlet extends BaseServlet {
    ExamPaperDAO dao = new ExamPaperDAO();

    /**
     * 展示所有班级专业院系
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void showClassDept(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();

        List<ClassMajorDeptShow> list = null;
        long count = 0;

        try {
            list = dao.showClassDept();

            count = dao.countClass();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        String res = gson.toJson(list);

        System.out.println(res);
        String reStr = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\": " + count + ",\n" +
                "  \"data\":" + res + "\n" +
                "} ";
        
        out.write(reStr);
    }

    /**
     * 展示所有试卷列表
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void showExams(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();
        List<ExamPaperShow> list = null;
        long count = 0;
        try {
            list = dao.showExams();
            count = dao.countquestion();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        String res = gson.toJson(list);
        String reStr = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\": " + count + ",\n" +
                "  \"data\": " + res + "\n" +
                "}";
        System.out.println(res);

        out.write(reStr);
    }

    // 我的试卷***********************************************************************************
    //分页显示所有试卷
    protected void allexam(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();

        int method = Integer.parseInt(request.getParameter("method"));
        //查出当前登录的用户id
//        HttpSession session = request.getSession();
//        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
//        System.out.println(user_id);
        List exampaperlist = null;
        int count = 0;
        List courselist1 = dao.findallcourse();
        if (method == 1) {
            System.out.println("显示一页试卷信息");
            exampaperlist = dao.allmyexam(100001);
            count = (int) dao.countexampaper(100001);
            request.setAttribute("method", method);
        } else {
            System.out.println("按照所属科目分类查询试卷");
            int course = Integer.parseInt(request.getParameter("course"));
            exampaperlist = dao.findexampaperBycouseid(course, 100001);
            count = (int) dao.countexampaperBycouseid(course, 100001);
            request.setAttribute("method", method);
        }
        System.out.println(exampaperlist);
        System.out.println(courselist1);
        String list = gson.toJson(exampaperlist);
        String res = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\":" + count + ",\n" +
                "  \"data\":" + list + "\n" +
                "} ";
        System.out.println(res);
        request.setAttribute("courselist1", courselist1);
        out.print(res);
        out.flush();
        out.close();
    }

    //删除我的试卷通过试卷id
    protected void delexampaper(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //查出当前登录的用户id
//        HttpSession session = request.getSession();
//        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
//        System.out.println(user_id);
        System.out.println("删除试卷");
        int exampaper_id = Integer.parseInt(request.getParameter("exampaper_id"));
        System.out.println(exampaper_id);
        dao.delexampaper(exampaper_id);
        //去哪？去查第一页
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/exampaper.jsp");
    }

    //    共享试卷***********************************************************************************
    //分页显示所有共享试卷 http://localhost:8080/OnlineExam/admin/exampaper?m=allshareexam&p=1&method=1/2
    protected void allshareexam(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();
        int method = Integer.parseInt(request.getParameter("method"));
        List shareexampaperlist = null;
        int count = 0;
        if (method == 1) {
            System.out.println("显示所有共享试卷信息");
            shareexampaperlist = dao.allshareexam();
            count = (int) dao.countByexampapertype();
            request.setAttribute("method", method);
        } else {
            System.out.println("按照条件分页查询共享试卷");
            String exampaper_name = request.getParameter("exampaper_name");
            int course_id = Integer.parseInt(request.getParameter("course_id"));
            int createuser_id = Integer.parseInt(request.getParameter("createuser_id"));
            System.out.println("接受ajax请求" + method + "--" + exampaper_name + "--" + course_id + "--" + createuser_id);
            shareexampaperlist = dao.shareexambyid(exampaper_name, course_id, createuser_id);
            if (shareexampaperlist == null) {
                System.out.println("不存在此试卷");
            }
            count = (int) dao.countByid(exampaper_name, course_id, createuser_id);
            request.setAttribute("method", method);
        }
        System.out.println(shareexampaperlist);
        String list = gson.toJson(shareexampaperlist);
        String res = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\":" + count + ",\n" +
                "  \"data\":" + list + "\n" +
                "} ";
        System.out.println(res);
        out.print(res);
        out.flush();
        out.close();
    }

    //查询所有题库  显示在下拉框
    protected void findallcourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("查询所有的题库");
      /*  request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        String test=request.getParameter("test");
        System.out.println("ajax获取数据"+test);
        Gson gson = new Gson();*/
        List allcourselist = dao.findallcourse();

//        String list = gson.toJson(allcourselist);
        System.out.println(allcourselist);

/*        out.print(list);
        out.flush();
        out.close();*/
    }

    //*******************************创建试卷*******************************************
    //创建试卷
    public void createxampaper(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("创建试卷");
        //        HttpSession session = request.getSession();
//        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
//        System.out.println(user_id);
        String exampaper_name = request.getParameter("exampapername");
        exampaper_name = new String(exampaper_name.getBytes("ISO-8859-1"), "utf-8");
        int course_id = Integer.parseInt(request.getParameter("course"));
        int exampaper_type = Integer.parseInt(request.getParameter("exampaper_type"));
        int exampaper_score = Integer.parseInt(request.getParameter("exampaper_score"));
        String exampaper_desc = request.getParameter("exampaper_desc");
        exampaper_desc = new String(exampaper_desc.getBytes("ISO-8859-1"), "utf-8");
        ExamPaper examPaper = new ExamPaper(100001, course_id, exampaper_name, exampaper_desc, exampaper_score, exampaper_type);
        int n = dao.createexampaper(examPaper);
        System.out.println(n);
        //去哪？去查第一页
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/exampaper.jsp");
    }

    //查询所有的试题
    public void findallquestion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();
        int method = Integer.parseInt(request.getParameter("method"));
        List questionlist = null;
        int count = 0;
        List courselist2 = dao.findallcourse();
        if (method == 1) {
            System.out.println("查询所有的试题");
            questionlist = dao.findallquestion();
            count = (int) dao.countquestion();
            request.setAttribute("method", method);
        } else {
            System.out.println("按条件查询试题");
            String question_type = request.getParameter("questiontype");
            int course_id = Integer.parseInt(request.getParameter("course"));
            questionlist = dao.findquestionbyid(question_type, course_id);
            count = (int) dao.countquestionBycouseid(question_type, course_id);
            request.setAttribute("method", method);
        }
        System.out.println(questionlist);
        System.out.println(courselist2);
        String list = gson.toJson(questionlist);
        String res = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\":" + count + ",\n" +
                "  \"data\":" + list + "\n" +
                "} ";
        System.out.println(res);
        request.setAttribute("courselist2", courselist2);
        out.print(res);
        out.flush();
        out.close();
    }


    //*******************************创建试卷*******************************************
    //显示要修改试卷原内容
    public void showupdateexampaper(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("显示修改试卷原内容");
        String exampaper_name = request.getParameter("exampapername");
//        exampaper_name = new String(exampaper_name.getBytes("ISO-8859-1"), "utf-8");
 /*               List<ExamPaper> exampaper= dao.findexamidbyname(exampaper_name);
        System.out.println(exampaper.get(0).getExampaper_id());
        int exampaper_id=exampaper.get(0).getExampaper_id();*/
        int exampaper_id = Integer.parseInt(request.getParameter("exampaperid"));//为什么传不过来
        System.out.println("id=" + exampaper_id);
        int course_id = Integer.parseInt(request.getParameter("course"));
        int exampaper_type = Integer.parseInt(request.getParameter("exampaper_type"));
        int exampaper_score = Integer.parseInt(request.getParameter("exampaper_score"));
        String exampaper_desc = request.getParameter("exampaper_desc");
//        exampaper_desc = new String(exampaper_desc.getBytes("ISO-8859-1"), "utf-8");
//        List<ExamPaper> updateexampaperlist=null;
        ExamPaper examPaperupdate = new ExamPaper(course_id, exampaper_name, exampaper_desc, exampaper_score, exampaper_type);
//        updateexampaperlist.add(examPaper);
        System.out.println(examPaperupdate);
        request.setAttribute("updateexampaper", examPaperupdate);
        request.setAttribute("updateexampaperid", exampaper_id);
        request.getRequestDispatcher("/admin/updateexampaper.jsp").forward(request, response);
    }

    //修改试卷
    public void updateexampaper(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("修改试卷");
        //        HttpSession session = request.getSession();
//        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
//        System.out.println(user_id);

        String exampaper_name = request.getParameter("exampapername");
        exampaper_name = new String(exampaper_name.getBytes("ISO-8859-1"), "utf-8");
//        List<ExamPaper> exampaper= dao.findexamidbyname(exampaper_name);
//        System.out.println(exampaper.get(0).getExampaper_id());
//        int exampaper_id=exampaper.get(0).getExampaper_id();
        int exampaper_id = Integer.parseInt(request.getParameter("exampaper_id"));
        System.out.println("id=" + exampaper_id);
        int course_id = Integer.parseInt(request.getParameter("course"));
        int exampaper_type = Integer.parseInt(request.getParameter("exampaper_type"));
        int exampaper_score = Integer.parseInt(request.getParameter("exampaper_score"));
        String exampaper_desc = request.getParameter("exampaper_desc");
        exampaper_desc = new String(exampaper_desc.getBytes("ISO-8859-1"), "utf-8");
        ExamPaper examPaper = new ExamPaper(course_id, exampaper_name, exampaper_desc, exampaper_score, exampaper_type);
        System.out.println(examPaper.getExampaper_id());
        int n = dao.updateexampaper(exampaper_id, examPaper);
        System.out.println("修改成功：" + n);
        //去哪？去查第一页
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/exampaper.jsp");
    }

}
